<?php
/* @var $this AnalysisController */
//当前商家运费模板
$express= Express::model()->getPressByActiveUser();

Yii::app()->clientScript->registerScript('search', "
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('product-grid', {
		data: $(this).serialize()
	});
	return false;
});
");

?>
<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'proPromotion-form',
		'enableAjaxValidation'=>false,
	)); ?>
	<ul class="form_list form_list2 discount create_box">
		<li class="c_f title"><h3 class="fs_14"><?php echo ($model->isNewRecord)?"创建":"修改"?>搭配套餐</h3></li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>套餐标题：</span>
			<div class="cont f_l">
				<?php echo $form->textField($model,'zpp_title',array('class'=>'t_ipt w_300','maxlength'=>30))?>
				<span id="ProPackage_zpp_titleTip"></span>
			</div>
		</li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>搭配宝贝：</span>
			<div class="cont f_l">
				<div class="match_box">
					<table width="100%" cellpadding="0" cellspacing="0" id="select_product_list">
						<tr>
							<th width="2%"></th>
							<th width="" class="tl">搭配宝贝</th>
							<th width="20%">原价</th>
							<th width="20%">操作</th>
							<th width="2%"></th>
						</tr>
						<tr class="no_add <?php if(!empty($model->products)) echo 'd_n' ?> no_bd">
							<td colspan="5" class="c_999">未添加搭配宝贝</td>
						</tr>
						<?php if(!empty($model->products)){ foreach($model->products as $val ){?>
							<tr class="prod" id="select_product_list_<?php echo $val->zp_id?>">
								<td class="no_border"><?php echo CHtml::hiddenField('ProPackage[zp_ids][]',$val->zp_id)?></td>
								<td class="pro tl">
									<div class="c_img c_img50"><?php echo CHtml::link(Tool::thumb($val->zp_pic,50,50,$val->zp_name))?></div>
									<p class="title_pro"><?php echo CHtml::link(mb_substr($val->zp_name,0,16,'utf8'),array('/home/product/detail','id'=>$val->zp_id),array('class'=>'c_06c'))?></p>
								</td>
								<td class="price"><?php echo $val->zp_price?></td>
								<td><a class="c_06c delete_select" href="javascript:" title="">删除</a></td>
								<td class="no_border"></td>
							</tr>
						<?php }}?>
					</table>
				</div>
				<p><a class="mem_bgx link_btn mr10" onclick="addmatch();" href="javascript:" title="">添加搭配宝贝<b class="mem_bgx left"></b><b class="mem_bgx right"></b></a><span class="c_999">至少要添加2件，至多添加5件宝贝</span></p>
			</div>
		</li>
		<li class="c_f"><span class="tit">套餐原价：</span>
			<div class="cont f_l"><input class="t_ipt w_120" id="package_price" type="text" readonly="readonly" name="" maxlength="10" />元</div>
		</li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>套餐一口价：</span>
			<div class="cont f_l">
				<?php echo $form->textField($model,'zpp_price',array('class'=>'t_ipt w_120','maxlength'=>10))?>元
				<span id="ProPackage_zpp_priceTip"></span>
			</div>
		</li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>套餐描述：</span>
			<div class="cont f_l">
				<?php  $this->widget('ext.keditor.KEditor',array(
			            'model'=>$model,  //传入form model
			            'name'=>'zpp_content', //设置name
			            'properties'=>array(
			                //设置接收文件上传的action
			                'uploadJson'=>'/Attachment/GetAjax',
			                //设置浏览服务器文件的action，这两个就是上面配置在/admin/default的
			             //   'fileManagerJson'=>'/Attachment/GetAjax',
			                'newlineTag'=>'br',
			                'allowFileManager'=>true,
			                //传值前加js:来标记这些是js代码
			                'afterCreate'=>"js:function() {
			                        K('#ChapterForm_all_len').val(this.count());
			                        K('#ChapterForm_word_len').val(this.count('text'));
			                    }",
			                'afterChange'=>"js:function() {
			                        K('#ChapterForm_all_len').val(this.count());
			                        K('#ChapterForm_word_len').val(this.count('text'));
			                    }",
			            ),
			            'textareaOptions'=>array(
			                'style'=>'width:635px;height:261px;',
			            )
				  )); ?>
			</div>
		</li>
		<li class="c_f"><span class="tit">运费设置：</span>
		  <div class="cont f_l">
				<ul class="sub_li">
					<?php echo $form->radioButtonList($model,'zpp_exp_status',Product::model()->getExpStatus(),array('template'=>'<li>{input} {label}</li>','onclick'=>'templateshow()'));?>
					<li <?php if($model->zpp_exp_status == 2) echo "class='select'"?> >
						<div class="template">
							<ul class="c_f">
								<?php echo $form->radioButtonList($model,'zel_id',$express,array('separator'=>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;','onclick'=>'select()'))?>
							</ul>
						</div>
					</li>
				</ul>
		  </div>
		</li>
		<li class="c_f save_link"><span class="tit">&nbsp;</span>
			<div class="cont f_l"><input class="mem_btn mem_btn20 mr5" type="submit" name="" value="发布套餐" /><input class="mem_btn mem_btn07" type="button" name="" value="取 消" /></div>
		</li>
	</ul>
<?php $this->endWidget(); ?>



<div class="d_n" id="product_list">
<div class="d-title" style="display: block; ">选择产品</div>
<a class="d-close" href="javascript:" style="display: block;" onclick="addmatch()">×</a>
<div class="pro_filter c_f search-form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'action'=>Yii::app()->createUrl($this->route),
	'method'=>'get',
)); ?>
	<label>产品标题：</label>
	<?php echo $form->textField($product,'zp_name',array('class'=>'t_ipt w_180'))?>
	<label>产品货号：</label>
	<?php echo $form->textField($product,'pro_no',array('class'=>'t_ipt w_180'))?>
	<?php echo CHtml::submitButton(Yii::t('cmp','Search'),array('class'=>'mem_bgx mem_btn01'))?>
<?php $this->endWidget(); ?>
</div>

<div style="height:300px;overflow-y:auto;">
<!-- 列表 -->
<?php 
	$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'product-grid',
	'dataProvider'=>$product->search(40),//数据源
	//'filter'=>$product,//设置过滤器，筛选输入框
	'template'=>'{items}',
	'enablePagination'=>false,
	'columns'=>array(
		array(
			'class' => 'CCheckBoxColumn',
			'checkBoxHtmlOptions' => array('name' => 'id[]','class'=>'chk d_n'),
			'htmlOptions'=>array('width'=>'6%'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'产口图片',
				'labelExpression'=>'Tool::thumb($data->zp_pic,50,50,$data->zp_name)',
				'urlExpression'=>'array("/home/product/detail","id"=>$data->zp_id)',
				'linkHtmlOptions'=>array('width'=>'50','height'=>'50'),
				'htmlOptions'=>array('width'=>'10%'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'产品标题',
				'labelExpression'=>'mb_substr($data->zp_name,0,12,"utf8")',
				'urlExpression'=>'array("/home/product/detail","id"=>$data->zp_id)',
				'linkHtmlOptions'=>array('class'=>'c_06c'),
				'htmlOptions'=>array('width'=>'30%','class'=>'t_c'),
		),
		array('name'=>'pro_no',
				'value'=>'$data->pro_no',
				'htmlOptions'=>array('width'=>'10%','class'=>'t_c'),
		),
		array('name'=>'zp_price',
				'value'=>'$data->zp_price',
				'htmlOptions'=>array('width'=>'15%','class'=>'t_c'),
		),
		array('name'=>'zp_inventory',
				'value'=>'$data->zp_inventory',
				'htmlOptions'=>array('width'=>'15%','class'=>'t_c'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'操作',
				'labelExpression'=>'"选择"',
				'urlExpression'=>'"javascript:"',
				'linkHtmlOptions'=>array('class'=>'mem_btn mem_btn13 pro_item'),
				'htmlOptions'=>array('width'=>'14%'),
		),
	)
));
?>
</div>
<div class="d-footer">
<div class="t_l f_l" id="select_pro" style="padding:10px 20px;">
<?php if(!empty($model->products)){ foreach($model->products as $val){?>
<span class="mr10 pro" id="select_pro_<?php echo $val->zp_id?>" pid="<?php echo $val->zp_id?>"><?php echo mb_substr($val->zp_name,0,16,'utf8')?></span>
<?php }}?>
</div>
<div class="d-buttons" style=""><input type="button" class="d-button d-state-highlight" value="确定" onclick="addmatch()"></div>
</div>
</div>
<script type="text/javascript">
//显示运费模板
function templateshow(){
	if($('#ProPackage_zpp_exp_status_1').attr('checked')){
		$('.template').parent().addClass("select");
	}else{
		$('.template').parent().removeClass("select");
	}
}

//选择
$('.pro_item').live('click',function(){
	var pid = $(this).parent().parent().find('.chk').val()
	var pic = $(this).parent().parent().find('td').eq(1).html();
	var pname = $(this).parent().parent().find('td').eq(2).html();
	var pnametext = $(this).parent().parent().find('td').eq(2).text();
	var pprice = $(this).parent().parent().find('td').eq(4).html();
	if($(this).text()=='删除'){
		$('#select_pro_'+pid).remove();
		$('#select_product_list_'+pid).remove();
		$(this).text('选择');
		$(this).removeClass('c_06c');
		$(this).addClass('mem_btn mem_btn13');
		$(this).prev('label').remove();
		if(!($('#select_product_list').find('.prod').eq(0).length>0)){
			$('#select_product_list .no_add').show();
		}
		updataPrice();
	}else{
		if($('#select_product_list .prod').length>4){
			art.dialog({title:'警告：',content:'只能最多选择五件产品',okValue:"确定",ok:function(){return true;}});
		}else{
			if($('#select_product_list_'+pid).length>0){
				art.dialog({title:'警告：',content:'相同产品请勿重复添加',okValue:"确定",ok:function(){return true;}});
				$(this).text('删除');
				$(this).removeClass('mem_btn mem_btn13');
				$(this).addClass('c_06c');
				$(this).before('<label class="mr10">已选择</label>');
			}else{
				$('#select_pro').append('<span class="mr10 pro" id="select_pro_'+pid+'" pid="'+pid+'">'+pnametext+'</span>');
				$('#select_product_list').append('<tr class="prod" id="select_product_list_'+pid+'"><td class="no_border"><input type="hidden" name="ProPackage[zp_ids][]" value="'+pid+'"></td><td class="pro tl">'
												+'<div class="c_img c_img50">'+pic+'</div><p class="title_pro">'+pname+'</p>'
												+'</td><td class="price">'+pprice+'</td><td><a class="c_06c delete_select" href="javascript:" title="">删除</a></td><td class="no_border"></td></tr>');
				$(this).text('删除');
				$(this).removeClass('mem_btn mem_btn13');
				$(this).addClass('c_06c');
				$(this).before('<label class="mr10">已选择</label>');
				$('#select_product_list .no_add').hide();
				$('#select_product_list').removeClass('error');
				updataPrice();
			}
		}
	}
})

//显示弹框
function addmatch(){
	$('#product_list').toggle();
}

//删除选择
$('.delete_select').live('click',function(){
	var pid = $(this).parent().parent().find('input').val();
	$('#select_pro_'+pid).remove();
	$('#select_product_list_'+pid).remove();
	if(!($('#select_product_list').find('.prod').eq(0).length>0)){
		$('#select_product_list .no_add').show();
	}
	updataPrice();
});
$('#select_pro .pro').live('click',function(){
	var pid = $(this).attr('pid');
	$('#select_pro_'+pid).remove();
	$('#select_product_list_'+pid).remove();
	if(!($('#select_product_list').find('.prod').eq(0).length>0)){
		$('#select_product_list .no_add').show();
	}
	updataPrice();
});

//更新套餐价格
function updataPrice(){
	var pricetd = $('#select_product_list .price');
	var price = 0;
	for(var i=0;i<pricetd.length;i++){
		price -= -$('#select_product_list .price').eq(i).text();
	}
	$('#package_price').val(price);
}

$(function(){
	updataPrice();
	$('#product_list').draggable({ handle: '.d-title' });
});

//提交前的验证，判断有没有选择产品
$('#proPromotion-form').bind("submit", function() {
    if($('#select_product_list .prod').length<2){
		$('#select_product_list').addClass('error');
		return false;
    }
    return true;
});
</script>
 
<!-- 表单验证 -->
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidator-4.1.1.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidatorRegex.js"></script>
 <script>
	jQuery(document).ready(function($) {
		$.formValidator.initConfig({theme:"Default",formID:"proPromotion-form",
			onError:function(msg, obj){
						alert(msg);
						return false;
					},
			onsuccess:function(){ return true; }
		});
	})
</script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/cmpValidate.js"></script> 